<template>
    <view class="container">
        <base-nav-bar></base-nav-bar>
        <scroll-view scroll-y="true">
            <uni-list-chat
                :clickable="true"
                @click="intoChat(item)"
                v-for="item in convs"
                :key="item.conversationID"
                :showBadge="!!item.unreadMessageCount"
                :badgeText="
                    item.notificationStatus == 2 && item.type == 2
                        ? 0
                        : (item.unreadMessageCount > 0 && item.unreadMessageCount) || 0
                "
                badge-positon="left"
                badge-text="dot"
                :title="item | transConvTitle"
                :avatar="item.conversationAvatarUrl"
                :note="item | formatLastMessage"
            >
                <view style="text-align: right; font-size: small">
                    <view class="chat-custom-text">{{ item | formatTime }}</view>
                    <uni-icons
                        v-if="item.type == 2"
                        :type="item.notificationStatus == 2 ? 'eye-slash-filled' : 'eye'"
                        color="#999"
                        size="18"
                        @click.native.stop="setConversationNotificationStatus(item)"
                    ></uni-icons>
                </view>
            </uni-list-chat>
        </scroll-view>
    </view>
</template>

<script>
function timestampToTime(timestamp) {
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
}

import { avatarPrefix } from '../../assets/js/config';

export default {
    data() {
        return {};
    },
    computed: {
        convs() {
            console.log(wx.$store.state.convs,"---------------1111")
            return wx.$store.state.convs;
        },
    },
    filters: {
        transConvTitle: function (item) {
            return `${item.conversationName || item.conversationID}`;
        },
        formatLastMessage: function (item) {
            return item.lastMessage && item.lastMessage.message
                ? item.lastMessage.message.length > 20
                    ? item.lastMessage.message.slice(0, 20) + '...'
                    : item.lastMessage.message
                : '';
        },
        formatTime(item) {
            if (item.lastMessage) {
                return timestampToTime(item.lastMessage.timestamp);
            }
            return '';
        },
    },
    onShow() {
        setTimeout(() => {
            wx.$store.queryConversationList();
        }, 500);
    },
    methods: {
        intoChat(item) {
            console.warn('intoChat', item);
            wx.$store.clearConversationUnreadMessageCount(item.conversationID, item.type).then(() => {
                console.log(item);
                const convInfo = {
                    id: item.conversationID,
                    type: item.type,
                    name: item.conversationName,
                };
                const paramsString =
                    '?' +
                    Object.keys(convInfo)
                        .map((key) => `${key}=${convInfo[key]}`)
                        .join('&');
                uni.navigateTo({
                    url: '/pages/chat/chat' + paramsString,
                });
            });
        },
        setConversationNotificationStatus(item) {
            console.warn('setConversationNotificationStatus', item);
            const status = item.notificationStatus == 2 ? 1 : 2;
            wx.$store.setConversationNotificationStatus(status, item.conversationID, 2).then(() => {
                wx.$store.queryConversationList();
            });
        },
        getConvAvatar(convID, convType) {
            if (convType == 2) return avatarPrefix + 'group.jpeg';
            const img = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png';
            return wx.$store.avatarMap[convID] || img;
        },
    },
};
</script>

<style lang="scss">
</style>
